package com.example.imgrecognition;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;

import com.example.imgrecognition.Util.ToastUtil;
import com.tencent.bugly.Bugly;

import org.litepal.LitePalApplication;
import org.xutils.x;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by zhoukaich on 2018/8/13.
 */

public class MyApplication extends Application {

    public static MyApplication instance;
    private List<Activity> activitys = null;
    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        x.Ext.init(this);
        x.Ext.setDebug(true);
        LitePalApplication.initialize(this);
        this.activitys = new LinkedList<Activity>();
        Bugly.init(getApplicationContext(), "d8c54da9b3", true);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this) ;
    }

    // 添加Activity到容器中
    public void addActivity(Activity activity) {
        if (activitys != null && activitys.size() > 0) {
            if(!activitys.contains(activity)){
                activitys.add(activity);
            }
        }else{
            activitys.add(activity);
        }

    }

    // 遍历所有Activity并finish
    public void exit() {
        if (activitys != null && activitys.size() > 0) {
            for (Activity activity : activitys) {
                activity.finish();
            }
//            ToastUtil.show(getApplicationContext(),"清除所有activity");
        }
        //System.exit(0);
    }
    public static MyApplication getInstance() {
        if (null == instance) {
            instance = new MyApplication();
        }
        return instance;

    }
}
